package com.ogms.dge.container.modules.data.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.ogms.dge.container.common.utils.PageUtils;
import com.ogms.dge.container.common.utils.Query;
import com.ogms.dge.container.modules.common.service.DataSourceService;
import com.ogms.dge.container.modules.data.converter.ConfigConverter;
import com.ogms.dge.container.modules.data.converter.ServiceConverter;
import com.ogms.dge.container.modules.data.dao.ServiceDao;
import com.ogms.dge.container.modules.data.dto.InstanceDto;
import com.ogms.dge.container.modules.data.entity.ServiceEntity;
import com.ogms.dge.container.modules.data.entity.SourceEntity;
import com.ogms.dge.container.modules.data.service.ServiceService;
import com.ogms.dge.container.modules.data.service.SourceService;
import com.ogms.dge.container.modules.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;


@Service("serviceService")
public class ServiceServiceImpl extends ServiceImpl<ServiceDao, ServiceEntity> implements ServiceService {
    @Resource
    private ServiceConverter serviceConverter;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SourceService sourceService;

    @Resource
    private ConfigConverter configConverter;

    @Autowired
    private DataSourceService dataSourceService;

    @Value("${container.data.dsd}")
    private String data_dsd;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ServiceEntity> page = this.page(
                new Query<ServiceEntity>().getPage(params),
                new QueryWrapper<ServiceEntity>()
                        .eq(params.containsKey("srcUuid"), "src_uuid", params.get("srcUuid"))
                        .eq(params.containsKey("typeId"), "type_id", params.get("typeId"))
                        .like(params.containsKey("key"), "name", params.get("key"))
        );
        PageUtils pageUtils = new PageUtils(page);
        pageUtils.setList(serviceConverter.poList2VoList(page.getRecords(), sysUserService, sourceService, dataSourceService));

        return pageUtils;
    }

    @Override
    public void delBySrcUuid(String srcUuid) {
        QueryWrapper<ServiceEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("src_uuid", srcUuid);
        this.remove(queryWrapper);
    }

    @Override
    public void delBySrcId(Long srcId) {
        delBySrcUuid(sourceService.getById(srcId).getUuid());
    }

    @Override
    public boolean saveBatch(String srcUuid, Long userId, List<Map<String, Object>> instances) throws JsonProcessingException {
        for (int i = 0; i < instances.size(); i++) {
            Integer methodId = (Integer) instances.get(i).get("methodId");
            String path = (String) instances.get(i).get("path");
            InstanceDto insDto = configConverter.map2InsDto(instances.get(i));
            ServiceEntity service = configConverter.insDto2Po(insDto, dataSourceService);
            service.setUuid(UUID.randomUUID().toString());
            service.setCreateTime(new Date());
            service.setMethodId(Long.valueOf(methodId));
            service.setPath(path);
            service.setSrcUuid(srcUuid);
            service.setCreateUserId(userId);
            service.setUseCount(0L);
            save(service);
        }
        return true;
    }

    @Override
    public String getBasePath(String serviceUuid) {
        ServiceEntity service = getOne(new QueryWrapper<ServiceEntity>().eq("uuid", serviceUuid));
        SourceEntity source = sourceService.getOne(new QueryWrapper<SourceEntity>().eq("uuid", service.getSrcUuid()));
        String baseUrl = "";
        if (source.getIsLocalPath()) {
            baseUrl = source.getLocalPath();
        } else {
            baseUrl = new File(data_dsd, source.getUuid()).getAbsolutePath();
        }
        return baseUrl;
    }
}